package Client.GUI;

import Client.Net.*;
import Client.main;
import javax.swing.*;

/**
 *
 */
public class frmClient extends javax.swing.JFrame implements ICommunication
{
    protected JPanel m_Panel;
    protected ClientService m_Service;
    
    /** 
     * Creates new form frmClient 
     */
    public frmClient()
    {
        initComponents();
        SetContent(new pnlLogin(this));
        setSize(1024, 768);
        try
        {
            // Initialize Service
            m_Service = new ClientService(main.getSettings().getLoginServerAddress(), main.getSettings().getLoginServerPort(), this);
        }
        catch (Exception ex)
        {
            JOptionPane.showConfirmDialog(this, "Cannot Create Network Service (" + ex.getMessage() + " / " + 
                    main.getSettings().getLoginServerAddress() + ":" + 
                    main.getSettings().getLoginServerPort() + ").", "Failed to Connect", 0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Risk client game");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                onResize(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 637, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 445, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Zet de grote van de panel op een vaste waarde
     * @param evt
     */
    private void onResize(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_onResize
        // Update panel when resized
        
        if (m_Panel != null)
        {
           // System.out.println("panel onResize " + m_Panel.toString());
            //m_Panel.setSize(getWidth(), getHeight());
            //m_Panel.setSize(1024,768);
            //setSize(1024, 768);
	    setSize(m_Panel.getWidth(), m_Panel.getHeight());
        }
	else
	    setSize(1024, 768);
    }

    /**
     * Geeft het huidige panel terug
     */
    public JPanel getContent()
    {
	return m_Panel;
    }
    
    /**
     * Deze methode zet het panel op een vaste waarde
     * @param panel Naam van de klasse
     */
    public void SetContent(JPanel panel)
    {
	if (m_Panel != null)
	{
	  m_Panel.setVisible(false);
	  remove(m_Panel);
	  
	  if (m_Panel instanceof Library.IDisposable)
	      ((Library.IDisposable)m_Panel).dispose();
	}
	//System.out.println("panel SetContent " + panel.toString() + panel.getWidth()+ panel.getHeight());
	//setSize(panel.getWidth(), panel.getHeight());
	//panel.setSize(panel.getSize());
	//setSize(1138, 838);

	panel.getClass().getName().toString();
	panel.setLocation(0, 0);
	this.add(panel);
	panel.setSize(panel.getPreferredSize());
	panel.setVisible(true);
	m_Panel = panel;
	setSize(m_Panel.getWidth(), m_Panel.getHeight());	
	this.invalidate();
    }

    public void log(String message)
    {
        System.out.println(message);
    }

    public void log(int errorcode, String message)
    {
        System.out.println(errorcode + " " + message);
    }
    
    public void logException(Throwable ex)
    {
        ex.printStackTrace();
    }
    
    public ClientService getService()
    {
        return m_Service;
    }

    @Override
    public void dispose()
    {
        if (m_Service != null)
        {
            m_Service.dispose();
            m_Service = null;
        }
        super.dispose();
    }//GEN-LAST:event_onResize

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // Het raampje wordt afgesloten, we geven door aan de servers dat we uitloggen
        if (m_Service != null)
            m_Service.dispose();
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
